/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.models.beans;

import com.models.entities.TblRoom;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Administrator
 */
@Stateless
public class RoomSessionBean {

    @PersistenceContext(unitName = "cungonbaiPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    public List<TblRoom> loadAllRoom() {
        Query query = em.createNamedQuery("TblRoom.findAll");
        return query.getResultList();
    }

    public boolean createRoom(TblRoom room) {
        try {
            persist(room);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }

    public void deleteRoom(int roomId) {
        TblRoom room = em.find(TblRoom.class, roomId);
        em.remove(room);
    }

    public TblRoom findRoomById(int roomId) {
        Query query = em.createQuery("SELECT * FROM tblRoom WHERE roomId = ?");
        query.setParameter(1, roomId);
        TblRoom room = new TblRoom();
        try {
            room = (TblRoom) query.getSingleResult();
            return room;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public List<TblRoom> findRoomByRoomName(String roomName){
        Query query = em.createNamedQuery("TblRoom.findByRoomName");
        query.setParameter("roomName", roomName);
        return query.getResultList();
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
